<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 基础语法</h1>
        <h2>编码</h2>

        <p>
            默认情况下，Python 3 源码文件以 <b>UTF-8</b> 编码，所有字符串都是 unicode 字符串。
            当然你也可以为源码文件指定不同的编码：
        </p>

        <pre class="prettyprint prettyprinted" style=""><span class="com"># -*- coding: cp-1252 -*-</span></pre>

        <h2>标识符</h2>
        <ul>
            <li>第一个字符必须是字母表中字母或下划线'_'。</li>
            <li>标识符的其他的部分有字母、数字和下划线组成。</li>
            <li>标识符对大小写敏感。</li>
        </ul>
        <p>
            在Python 3中，非-ASCII 标识符也是允许的了。
        </p>

        <h2>python保留字</h2>
        <p>

            保留字即关键字，我们不能把它们用作任何标识符名称。Python 的标准库提供了一个 keyword 模块，可以输出当前版本的所有关键字：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> keyword
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> keyword</span><span class="pun">.</span><span
                class="pln">kwlist
</span><span class="pun">[</span><span class="str">'False'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'None'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'True'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'and'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'as'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'assert'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'break'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'class'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'continue'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'def'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'del'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'elif'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'else'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'except'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'finally'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'for'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'from'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'global'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'if'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'import'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'in'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'is'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'lambda'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'nonlocal'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'not'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'or'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'pass'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'raise'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'return'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'try'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'while'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'with'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'yield'</span><span class="pun">]</span></pre>



        <h2>注释</h2>
        <p>
            Python中单行注释以 <b>#</b> 开头，实例如下：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 第一个注释</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Hello, Python!"</span><span
                class="pun">)</span><span class="pln"> </span><span class="com"># 第二个注释</span></pre>
        <p>执行以上代码，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Hello</span><span class="pun">,</span><span
                class="pln"> </span><span class="typ">Python</span><span class="pun">!</span></pre>
        <p>多行注释可以用多个 <span class="marked">#</span> 号，还有 <span class="marked">'''</span> 和 <span
                class="marked">"""</span>：</p>

        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="com"># 第一个注释</span><span class="pln">
</span><span class="com"># 第二个注释</span><span class="pln">

</span><span class="str">'''
第三注释
第四注释
'''</span><span class="pln">

</span><span class="str">"""
第五注释
第六注释
"""</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Hello, Python!"</span><span
                class="pun">)</span><span class="pln"> </span></pre>
        <p>执行以上代码，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Hello</span><span class="pun">,</span><span
                class="pln"> </span><span class="typ">Python</span><span class="pun">!</span></pre>

        <h2>行与缩进
        </h2>
        <p>
            python最具特色的就是使用缩进来表示代码块，不需要使用大括号 <span class="marked">{}</span> 。</p>
        <p>缩进的空格数是可变的，但是同一个代码块的语句必须包含相同的缩进空格数。实例如下：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">if</span><span class="pln"> </span><span
                class="kwd">True</span><span class="pun">:</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"True"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"False"</span><span class="pun">)</span></pre>
        <p>以下代码最后一行语句缩进数的空格数不一致，会导致运行错误：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">if</span><span class="pln"> </span><span
                class="kwd">True</span><span class="pun">:</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"Answer"</span><span class="pun">)</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"True"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">else</span><span class="pun">:</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"Answer"</span><span class="pun">)</span><span class="pln">
  </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"False"</span><span class="pun">)</span><span class="pln">    </span><span class="com"># 缩进不一致，会导致运行错误</span></pre>
        <p>以上程序由于缩进不一致，执行后会出现类似以下错误：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln"> </span><span class="typ">File</span><span
                class="pln"> </span><span class="str">"test.py"</span><span class="pun">,</span><span
                class="pln"> line </span><span class="lit">6</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"False"</span><span class="pun">)</span><span class="pln">    </span><span class="com"># 缩进不一致，会导致运行错误</span><span
                class="pln">
                                      </span><span class="pun">^</span><span class="pln">
</span><span class="typ">IndentationError</span><span class="pun">:</span><span class="pln"> unindent does </span><span
                class="kwd">not</span><span class="pln"> match any outer indentation level</span></pre>

        <h2>多行语句</h2>
        <p>Python 通常是一行写完一条语句，但如果语句很长，我们可以使用反斜杠(\)来实现多行语句，例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">total </span><span class="pun">=</span><span
                class="pln"> item_one </span><span class="pun">+</span><span class="pln"> \
        item_two </span><span class="pun">+</span><span class="pln"> \
        item_three</span></pre>
        <p>在 [], {}, 或 () 中的多行语句，不需要使用反斜杠(\)，例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">total </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">[</span><span class="str">'item_one'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'item_two'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'item_three'</span><span
                class="pun">,</span><span class="pln">
        </span><span class="str">'item_four'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'item_five'</span><span class="pun">]</span></pre>


        <h2>数据类型</h2>
        <p>python中数有四种类型：整数、长整数、浮点数和复数。</p>
        <ul>
            <li><strong>int</strong> (整数), 如 1</li>
            <li><strong>long</strong> (长整数) , 比较大的整数</li>
            <li><strong>float</strong> (浮点数), 如 1.23、3E-2</li>
            <li><strong>complex</strong> (复数), 如 1 + 2j、 1.1 + 2.2j</li>
        </ul>

        <h2>字符串</h2>
        <ul>
            <li><span style="color: #000000;">python中单引号和双引号使用完全相同。</span></li>
            <li><span style="color: #000000;">使用三引号('''或""")可以指定一个多行字符串。</span></li>
            <li><span style="color: #000000;">转义符 '\'</span></li>
            <li>自然字符串， 通过在字符串前加r或R。 如 r"this is a line with \n" 则\n会显示，并不是换行。</li>
            <li>python允许处理unicode字符串，加前缀u或U， 如 u"this is an unicode string"。</li>
            <li>字符串是不可变的。</li>
            <li>按字面意义级联字符串，如"this " "is " "string"会被自动转换为this is string。</li>
        </ul>

        <pre class="prettyprint prettyprinted" style=""><span class="pln">word </span><span class="pun">=</span><span
                class="pln"> </span><span class="str">'字符串'</span><span class="pln">
sentence </span><span class="pun">=</span><span class="pln"> </span><span class="str">"这是一个句子。"</span><span class="pln">
paragraph </span><span class="pun">=</span><span class="pln"> </span><span class="str">"""这是一个段落，
可以由多行组成"""</span></pre>

        <h2>空行</h2>
        <p>函数之间或类的方法之间用空行分隔，表示一段新的代码的开始。类和函数入口之间也用一行空行分隔，以突出函数入口的开始。</p>
        <p>空行与代码缩进不同，空行并不是Python语法的一部分。书写时不插入空行，Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码，便于日后代码的维护或重构。</p>
        <p><b>记住：</b>空行也是程序代码的一部分。</p>

        <h2>等待用户输入</h2>
        <p>执行下面的程序在按回车键后就会等待用户输入：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

input</span><span class="pun">(</span><span class="str">"\n\n按下 enter 键后退出。"</span><span class="pun">)</span></pre>
        <p>以上代码中 ，"\n\n"在结果输出前会输出两个新的空行。一旦用户按下 enter 键时，程序将退出。</p>

        <h2>同一行显示多条语句</h2>
        Python可以在同一行中使用多条语句，语句之间使用分号(;)分割，以下是一个简单的实例：<p></p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> sys</span><span class="pun">;</span><span
                class="pln"> x </span><span class="pun">=</span><span class="pln"> </span><span
                class="str">'runoob'</span><span class="pun">;</span><span class="pln"> sys</span><span
                class="pun">.</span><span class="pln">stdout</span><span class="pun">.</span><span
                class="pln">write</span><span class="pun">(</span><span class="pln">x </span><span
                class="pun">+</span><span class="pln"> </span><span class="str">'\n'</span><span
                class="pun">)</span></pre>
        <p>执行以上代码，输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">runoob
</span><span class="lit">7</span></pre>

        <h2>多个语句构成代码组</h2>
        <p>缩进相同的一组语句构成一个代码块，我们称之代码组。</p>
        <p>像if、while、def和class这样的复合语句，首行以关键字开始，以冒号( : )结束，该行之后的一行或多行代码构成代码组。</p>
        <p>我们将首行及后面的代码组称为一个子句(clause)。</p>
        <p>如下实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">if</span><span class="pln"> expression </span><span
                class="pun">:</span><span class="pln">
   suite
</span><span class="kwd">elif</span><span class="pln"> expression </span><span class="pun">:</span><span class="pln">
   suite
</span><span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span><span class="pln">
   suite</span></pre>

        <h2>Print 输出</h2>
        <p>print 默认输出是换行的，如果要实现不换行需要在变量末尾加上 <b>end=""</b>：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

x</span><span class="pun">=</span><span class="str">"a"</span><span class="pln">
y</span><span class="pun">=</span><span class="str">"b"</span><span class="pln">
</span><span class="com"># 换行输出</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln"> x </span><span
                class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln"> y </span><span
                class="pun">)</span><span class="pln">

</span><span class="kwd">print</span><span class="pun">(</span><span class="str">'---------'</span><span
                class="pun">)</span><span class="pln">
</span><span class="com"># 不换行输出</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln"> x</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">end</span><span
                class="pun">=</span><span class="str">" "</span><span class="pln"> </span><span
                class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">(</span><span class="pln"> y</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">end</span><span
                class="pun">=</span><span class="str">" "</span><span class="pln"> </span><span
                class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pun">()</span></pre>
        <p>以上实例执行结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">a
b
</span><span class="pun">---------</span><span class="pln">
a b</span></pre>

        <h2>import 与 from...import</h2>
        <p>在 python 用 <span class="marked">import</span> 或者 <span class="marked">from...import</span> 来导入相应的模块。</p>
        <p>
            将整个模块(somemodule)导入，格式为： <span class="marked">import somemodule</span></p>
        <p>
            从某个模块中导入某个函数,格式为： <span class="marked">from somemodule import somefunction</span></p>
        <p>
            从某个模块中导入多个函数,格式为： <span class="marked">from somemodule import firstfunc, secondfunc, thirdfunc</span></p>
        <p>
            将某个模块中的全部函数导入，格式为： <span class="marked">from somemodule import *</span></p>

        <div class="example">
            <div class="example">导入 sys 模块</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">sys</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">================Python import mode==========================</span><span
                        class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-string">命令行参数为:</span><span class="hl-quotes">'</span><span
                        class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span
                        class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-identifier">sys</span><span class="hl-code">.</span><span
                        class="hl-identifier">argv</span><span class="hl-code">:
    </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">i</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">'</span><span class="hl-special">\n</span><span
                        class="hl-string"> python 路径为</span><span class="hl-quotes">'</span><span
                        class="hl-code">,</span><span class="hl-identifier">sys</span><span
                        class="hl-code">.</span><span class="hl-identifier">path</span><span
                        class="hl-brackets">)</span></pre>
            </div>
        </div>


        <div class="example">
            <div class="example">导入 sys 模块的 argv,path 成员</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">from</span><span class="hl-code"> </span><span
                        class="hl-identifier">sys</span><span class="hl-code"> </span><span
                        class="hl-reserved">import</span><span class="hl-code"> </span><span
                        class="hl-identifier">argv</span><span class="hl-code">,</span><span
                        class="hl-identifier">path</span><span class="hl-code">  </span><span class="hl-comment">#  导入特定的成员</span><span
                        class="hl-code">

</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">================python from import===================================</span><span
                        class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span
                        class="hl-string">path:</span><span class="hl-quotes">'</span><span
                        class="hl-code">,</span><span class="hl-identifier">path</span><span
                        class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-comment"># 因为已经导入path成员，所以此处引用时不需要加sys.path</span>
                </pre>
            </div>
        </div>


        <h2>命令行参数</h2>
        <p>很多程序可以执行一些操作来查看一些基本信息，Python可以使用-h参数查看各参数帮助信息：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python </span><span
                class="pun">-</span><span class="pln">h
usage</span><span class="pun">:</span><span class="pln"> python </span><span class="pun">[</span><span class="pln">option</span><span
                class="pun">]</span><span class="pln"> </span><span class="pun">...</span><span
                class="pln"> </span><span class="pun">[-</span><span class="pln">c cmd </span><span class="pun">|</span><span
                class="pln"> </span><span class="pun">-</span><span class="pln">m mod </span><span
                class="pun">|</span><span class="pln"> file </span><span class="pun">|</span><span
                class="pln"> </span><span class="pun">-]</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">arg</span><span class="pun">]</span><span class="pln"> </span><span
                class="pun">...</span><span class="pln">
</span><span class="typ">Options</span><span class="pln"> </span><span class="kwd">and</span><span class="pln"> arguments </span><span
                class="pun">(</span><span class="kwd">and</span><span
                class="pln"> corresponding environment variables</span><span class="pun">):</span><span class="pln">
</span><span class="pun">-</span><span class="pln">c cmd </span><span class="pun">:</span><span class="pln"> program passed </span><span
                class="kwd">in</span><span class="pln"> </span><span class="kwd">as</span><span
                class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span
                class="pun">(</span><span class="pln">terminates option list</span><span class="pun">)</span><span
                class="pln">
</span><span class="pun">-</span><span class="pln">d     </span><span class="pun">:</span><span class="pln"> debug output </span><span
                class="kwd">from</span><span class="pln"> parser </span><span class="pun">(</span><span class="pln">also PYTHONDEBUG</span><span
                class="pun">=</span><span class="pln">x</span><span class="pun">)</span><span class="pln">
</span><span class="pun">-</span><span class="pln">E     </span><span class="pun">:</span><span class="pln"> ignore environment variables </span><span
                class="pun">(</span><span class="pln">such </span><span class="kwd">as</span><span class="pln"> PYTHONPATH</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">-</span><span class="pln">h     </span><span class="pun">:</span><span
                class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span
                class="kwd">this</span><span class="pln"> help message </span><span class="kwd">and</span><span
                class="pln"> </span><span class="kwd">exit</span><span class="pln">

</span><span class="pun">[</span><span class="pln"> etc</span><span class="pun">.</span><span class="pln"> </span><span
                class="pun">]</span></pre>
        <p>我们在使用脚本形式执行 Python 时，可以接收命令行输入的参数，具体使用可以参照 <a href="/python3/python3-command-line-arguments.html"
                                                         target="_blank">Python 3 命令行参数</a>。</p>

    </div>
</div>
</body>
</html>
